<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include('../../include/dbFunctions.php');
include ("../../include/image_slider.php");
include ("../../include/function.php");

if (isset($_SESSION['volunteer_id'])) {
//Set up variables
    $event_id = $_GET['id'];

    $_SESSION['event_id'] = $event_id; //Make $id become GLOBAL for passing to next page for future useage
    $volunteer_id = $_SESSION['volunteer_id'];


//Select event and program details
    $sQuery = "SELECT e.event_id, e.event_name, e.contact_id, e.program_id,e.event_status, p.program_name, p.program_lesson_duration
           FROM  event e,program p
           WHERE p.program_id = e.program_id
           AND event_id = $event_id";
    $event = executeSelectQuery($sQuery);

// Select school details
    $school = "SELECT s.school_name, s.school_address, s.school_postal
            FROM school s, contact_school c, event e
            WHERE c.contact_school_id = e.contact_id
            AND s.school_id = c.school_id
            AND e.event_id = $event_id
            ";
    $school = executeSelectQuery($school);

// Select session
    $session = "SELECT * FROM session WHERE event_id = $event_id";
    $session = executeSelectQuery($session);

// Select class
    $class_array = "SELECT * FROM class WHERE event_id = $event_id";
    $class_array = executeSelectQuery($class_array);

//checking whether user is able to request or agree with request event
    $exist = "SELECT COUNT(*) as count FROM trainer WHERE event_id =   $event_id  AND volunteer_id = $volunteer_id";
    $exist = executeSelectQuery($exist);

    $status = "SELECT event_status FROM event WHERE event_id = $event_id ";
    $status = executeSelectQuery($status);

    $request = "SELECT COUNT(*) as count FROM trainer WHERE event_id = $event_id AND volunteer_id = $volunteer_id AND trainer_status = 'requested' ";
    $request = executeSelectQuery($request);

    $confirm = "SELECT COUNT(*) as count FROM trainer WHERE event_id = $event_id AND volunteer_id = $volunteer_id AND trainer_status = 'confirmed' ";
    $confirm = executeSelectQuery($confirm);

    $accept = "SELECT COUNT(*) as count FROM trainer WHERE event_id = $event_id AND volunteer_id = $volunteer_id AND trainer_status = 'accept' ";
    $accept = executeSelectQuery($accept);
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Junior Achievement Singapore</title>
        <style type="text/css" title="currentStyle">
            @import "../../media/css/jqdialog.css";
        </style>
        <link href="../../media/css/styles.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" language="javascript" src="../../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="../../media/js/jqueryUI/jquery-ui-1.8.13.custom.js"></script>
        <script type="text/javascript" src="../../media/js/jquery/jqdialog.min.js"></script>
        <script type="text/javascript" src="../../media/js/jqueryUI/jquery.ui.dialog.js"></script>
        <link type="text/css" href="../../media/css/dataTable.css" rel="stylesheet" />
        <script type="text/javascript" language="javascript" src="../../media/js/jquery/dataTable.js"></script>

        <script type="text/javascript" src="../../media/js/jquery/image_slider.js"></script>
        <!-- Custom jquery scripts -->
        <script src="../../media/js/jquery/custom_jquery.js" type="text/javascript"></script>
        <script type="text/javascript" language="javascript" charset="utf-8">
            $(document).ready(function(){
                $("#reject").click(function(){
                    deleteRecord();
                });
                $('#eventtable1').dataTable( {
                    "sPaginationType": "full_numbers"
                } );
                $('#eventtable2').dataTable( {
                    "sPaginationType": "full_numbers"
                } );
            });
            function deleteRecord(){
                var backURL = "volunteerRequestList.php"
                $.jqDialog.confirm("Are you sure want to reject this event?",
                function() {
                    $.ajax({
                        type: 'GET',
                        url: 'volunteerRejectEvent.php',
                        success: function(data){
                            if(data!=0){
                                alert("Reject Successfully");
                                $(location).attr('href',backURL);
                            }else{
                                alert("An error was occured! Could not reject this event!");
                            }
                        }
                    });
                });
            };

        </script>
    </head>
    <body>
        <script>
            var timeout    = 500;
            var closetimer = 0;
            var ddmenuitem = 0;

            function jsddm_open()
            {  jsddm_canceltimer();
                jsddm_close();
                ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

            function jsddm_close()
            {  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

            function jsddm_timer()
            {  closetimer = window.setTimeout(jsddm_close, timeout);}

            function jsddm_canceltimer()
            {  if(closetimer)
                {  window.clearTimeout(closetimer);
                    closetimer = null;}}

            $(document).ready(function()
            {  $('#navigation > li').bind('mouseover', jsddm_open)
                $('#navigation > li').bind('mouseout',  jsddm_timer)});

            document.onclick = jsddm_close;

        </script>
        <div id="container"><!--CONTAINER STARTS-->
            <div id="navigation">
                <li><a href="../../include/home/homepage.php">HOME</a></li>
                <li><a href="../../include/home/contact.php">CONTACT</a></li>
                <li><a href="http://ja.org.sg/">ABOUT JA SINGAPORE</a></li>
                <li><a href="http://jascheduling.lefora.com/">FEEDBACK</a></li>
                <li><a href="../../guide/guide_menu.php">HELP</a></li>

            </div>
            <div id="title"><!--TITLE STARTS-->
            </div><!--TITLE ENDS-->
            <div id="featured"><!--FEATURED AREA STARTS-->
                <div class="featured-image"><!--FEATURED IMAGE STARTS-->
                    <img alt="Featured Image" src="../../media/images/featured_image.png"></img>
                </div><!--FEATURED IMAGE ENDS-->
                <div class="featured-text"><!--FEATURED TEXT STARTS-->
                    <h2>Welcome to JA Singapore</h2>
                    <p>Junior Achievement programs help prepare young people for the real world by showing them how to generate wealth and effectively manage it, how to create jobs which make their communities more robust, and how to apply entrepreneurial thinking to the workplace. <a href="http://www.jaworldwide.org/">Read More... </a></p>
                </div><!--FEATURED TEXT ENDS-->
            </div><!--FEATURED AREA ENDS-->
            <?php
            custom_login();
            ?>
            <div id="left-content"><!--RIGHT CONTENT STARTS-->
                <div class="left-top"><!--RIGHT CONTENT BOX TOP-->
                </div><!--RIGHT CONTENT BOX TOP ENDS-->
                <div class="left-middle"><!--RIGHT CONTENT BOX MIDDLE-->

                    <?php
                    if (!isset($_SESSION['volunteer_id'])) {
                        echo "<br/><br/><br/><br/><br/><center><h3>You don't have permission to view this page<h3><br/>
                      Please login to access the page.</center><br/><br/><br/><br/><br/><br/><br/><br/>";
                    } else {
                        ?>
                    <h3>Course Details</h3>
                    <!--Display events details-->
                        <?php
                        if (($exist[0]['count'] == 0 || $request[0]['count'] > 0 ) && $status[0]['event_status'] == 'pending') {
                            ?>
                    <p class="pmain">(Please click Acknowledge box and Submit button if you agree to participate in this course. If not, please click Reject button)</p><br/>
                            <?php
                        }
                        ?>
                    <table id="ongoing_events" class="tabledetail">
                        <tr>
                            <th class="tabledetailth">Course Name: </th>
                            <td><?php echo $event[0]['event_name'] ?></td>
                        </tr>
                        <tr>
                            <!--<th class="tabledetailth">Event Status: </th>
                            <td><?php echo $event[0]['event_status'] ?></td>-->
                        </tr>
                        <tr>
                            <th class="tabledetailth">Program Name: </th>
                            <td><?php echo $event[0]['program_name'] ?></td>
                        </tr>
                        <tr>
                            <th class="tabledetailth">School Name: </th>
                            <td><?php echo $school[0]['school_name'] ?></td>
                        </tr>
                        <tr>
                            <th class="tabledetailth">School Address: </th>
                            <td><?php echo $school[0]['school_address'] ?></td>
                        </tr>
                        <tr>
                            <th class="tabledetailth">School Postal Code: </th>
                            <td><?php echo $school[0]['school_postal'] ?></td>
                        </tr>
                    </table>
                    <br/><br/>
                    <h3>Sessions: </h3>
                    <table  cellpadding="0" cellspacing="0" border="0" class="display" id="eventtable1">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>Date</th>
                                <th>Start time</th>
                                <th>End time</th>
                            </tr>
                        </thead>
                        <tbody>
                                <?php
                                for ($i = 0; $i < count($session); $i++) {
                                    ?>
                            <tr>
                                <td><?php echo $i + 1; ?></td>
                                <td><?php echo $session[$i]['session_date'] ?></td>
                                <td><?php echo $session[$i]['session_time'] ?></td>
                                <td><?php echo $session[$i]['session_end_time'] ?></td>
                            </tr>
                                    <?php
                                }
                                ?>
                        </tbody>
                    </table >
                    <br/><br/><br/><br/>
                    <h3>Classes:</h3>
                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="eventtable2">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>Class Name</th>

                                <th>No. Student</th>
                                <th> Volunteers</th>

                            </tr>
                        </thead>
                        <tbody>
                                <?php
                                $class_array2 = array();
                                for ($i = 0; $i < count($class_array); $i++) {
                                    $no = $i + 1;
                                    $cid = $class_array[$i]['class_id'];
                                    $name = $class_array[$i]['class_name'];
                                    $no_student = $class_array[$i]['class_no_student'];
                                    $file = $class_array[$i]['class_student_list'];
                                    $classid = $class_array[$i]['class_id'];
                                    echo "<tr>";
                                    echo "<td>$no</td>";
                                    echo "<td>$name</td>";
                                    echo "<td>$no_student</td>";

                                    // get volunteer data
                                    $query2 = "SELECT v.volunteer_surname, v.volunteer_firstname, v.volunteer_id
                                                                                    FROM trainer t, volunteer v
                                                                                    WHERE t.volunteer_id = v.volunteer_id
                                                                                    AND t.trainer_status= 'confirmed'
                                                                                    AND t.event_id=$event_id
                                                                                    AND t.class_id =$cid";
                                    $trainer_array = executeSelectQuery($query2);

                                    for ($j = 0; $j < count($trainer_array); $j++) {
                                        $trainer_array[$j]['volunteer_name'] = $trainer_array[$j]['volunteer_firstname'] . " " . $trainer_array[$j]['volunteer_surname'];
                                    }
                                    echo "<td>";
                                    for ($t = 0; $t < count($trainer_array); $t++) {

                                        $trainer_name =  $trainer_array[$t]['volunteer_name'];
                                        echo $trainer_name . "<br>";
                                    }
                                    echo "</td>";

                                    echo "</tr>";
                                }
                                ?>
                        </tbody>
                    </table>
                    <br><br><br><br><br>
                                            <?php
                                            //Display choice to request or agree to participate in or reject event base on status

                                            if ($confirm[0]['count'] > 0) {
                                                echo "<br/>You have already been confirmed to be volunteer for this event!<br>";
                                            } elseif ($accept[0]['count']>0) {
                                                echo "<br/>You have already requested to be a volunteer for this event. Please wait for a confirmation or class assignment.";
                                                echo 'For any queries, please contact <a href ="mailto:hauyee.ng@ja.org"><font color="#0000FF">JA Singapore Director Ms. Ng Hau Yee</font></a> for more details.';

                                            }?><table>
                                            <tr><td><input type='button' value='' onclick='history.go(-1)' class="form-back"/></td>
                                                    <?php
                                                    if (($exist[0]['count'] == 0 || $request[0]['count'] > 0 ) && $status[0]['event_status'] == 'pending') {
                                                        ?>
                                                <form id="event" method="post" action="volunteerRequestEvent.php">
                                                            <?php
                                                            if ($exist[0]['count'] == 0) {
                                                                ?>
                                                    <br/> <input type ="checkbox" name ="agree" value ="exist" /> I will commit in this event.
                                                                <?php
                                                            }
                                                            if ($request[0]['count'] > 0) {
                                                                ?>
                                                    <br/> <input type ="checkbox" name ="agree" value ="request" /> I will commit in this event.
                                                                <?php
                                                            }
                                                            ?>
                                                    <br></br>
                                                    <td><input type ="submit" name ="submit" value ="" class="form-submit"/></td>
                                                </form>
                                                        <?php
                                                    }
                                                    if ($request[0]['count'] > 0) {
                                                        ?>
                                                <td><button  id ="reject"  value ="" class="form-reject">Reject</button></td>
                                                        <?php
                                                    }

                                                    ?>

                                            </tr></table>
                                        <br></br>

                                        For any query, please contact <a href ="mailto:hauyee.ng@ja.org"><font color='#0000FF'>JA Singapore Director Ms. Ng Hau Yee </font></a> for more detail


                                        </br></br>

                                            <?php
                                        }
                                        ?>

                                        </div><!--RIGHT CONTENT BOX MIDDLE ENDS-->
                                        <div class="left-bottom"><!--RIGHT CONTENT BOX BOTTOM-->
                                        </div><!--RIGHT CONTENT BOX BOTTOM ENDS-->
                                        </div><!--RIGHT CONTENT ENDS-->
                                        </div><!--CONTAINER ENDS-->
                                        <div id="footer"><!--FOOTER STARTS-->
                                            <p>Copyright &copy; Junior Achievement Singapore | All Rights Reserved</p>
                                        </div><!--FOOTER ENDS-->

                                        </body>
                                        </html>
